package com.jsoft.dao.impl;

import com.jsoft.dao.TicketEnterInstiDao;
import com.jsoft.entity.TicketOpen;
import com.jsoft.entity.po.TicketEnterInsti;
import com.jsoft.utils.DAO;
import com.mysql.jdbc.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * TicketEnterInstiDao的实现类
 *
 * @ClassName TicketEnterInstiDaoImpl
 * @Description TODO
 * @Author jindong
 * @Date 2023/2/22 22:47
 * @Version 1.0
 */

public class TicketEnterInstiDaoImpl extends DAO<TicketEnterInsti> implements TicketEnterInstiDao {
    /**
     * 查询所有的复合列表
     *
     * @return 返回所有的复合集合
     * @throws Exception
     */
    @Override
    public List<TicketEnterInsti> selectAll() throws Exception {
        String sql = "SELECT tid,`no`,enterpriseName,enterpriseCode,acquirerName,acquirerCode,amount,instutityName,create_time createTime,expiry_time expiryTime,payment_interest_type paymentInterestType,`status`,`remark`,uplink_address uplinkAddress \n" +
                "FROM TicketEnterInsti";
        return getForList(sql);
    }


    /**
     * 根据点击不同数据后的审核查询相对应的数据
     *
     * @return 一个TicketEnterInsti对象
     * @throws Exception
     */
    @Override
    public TicketEnterInsti selectTicketEnterInstiById(Integer tid) throws Exception {

        String sql = "SELECT tid,`no`,enterpriseName,enterpriseCode,acquirerName,acquirerCode,amount,instutityName,create_time createTime,expiry_time expiryTime,payment_interest_type paymentInterestType,`status`,uplink_address uplinkAddress, `remark`\n" +
                "FROM TicketEnterInsti\n" +
                "WHERE tid=?";
        return get(sql, tid);
    }

    /**
     * 根据查询的id将其该条数据的状态改为复核通过
     *
     * @param tid
     * @return 改变的条数
     * @throws Exception
     */
    @Override
    public int updateStatusById(Integer tid) throws Exception {
        String sql = "UPDATE  ticket_open SET `status`='A' WHERE id=?";
        return update(sql, tid);
    }

    /**
     * 根据查询的idtong将其该条数据的状态改为复核不通过
     *
     * @param tid
     * @return 改变的条数
     * @throws Exception
     */
    @Override
    public int changeStatusById(Integer tid) throws Exception {
        String sql = "UPDATE  ticket_open SET `status`='D' WHERE id=?";
        return update(sql, tid);
    }

    /**
     * 根据选择的条件不同返回满足条件的视图对象集合
     * @param no
     * @param acquirerName
     * @param minamount
     * @param maxamount
     * @param createTime
     * @param enterpriName
     * @return 试图对象集合
     * @throws Exception
     */
    @Override
    public List<TicketEnterInsti> CheckTicketByView(String no, String acquirerName, String minamount, String maxamount, Date createTime, String enterpriName) throws Exception {
        List queryList = new ArrayList();
        String sql = "select no, acquirerName,amount,enterpriseName,instutityName,create_time createTime,expiry_time expiryTime,uplink_address uplinkAddress from ticketenterinsti where 1=1 ";
        if (!StringUtils.isNullOrEmpty(no)) {
            sql += "and no like \"%\"?\"%\"";
            queryList.add(no);
        }
        if (!StringUtils.isNullOrEmpty(acquirerName)) {
            sql += "and acquirerName=?";
            queryList.add(acquirerName);
        }
        if (!StringUtils.isNullOrEmpty(enterpriName)) {
            sql += "and exterpriName=?";
            queryList.add(enterpriName);
        }
        if (!StringUtils.isNullOrEmpty(minamount) || !StringUtils.isNullOrEmpty(maxamount)) {
            sql += "and amount >=? and amount <=?";
            queryList.add(minamount);
            queryList.add(maxamount);

        }
        if (!Objects.isNull(createTime)) {
            sql += "and  create_time= ?";
            queryList.add(createTime);
        }
        return getForList(sql, queryList.toArray());
    }

    /**
     * 根据企业名查询付款列表
     * @return 返回付款列表
     * @throws Exception 将异常抛到servlet层
     */
    @Override
    public List<TicketEnterInsti> findOpenList() throws Exception {
        String sql = "SELECT tid,`no`,enterpriseName,enterpriseCode,acquirerName,acquirerCode,amount,instutityName,create_time createTime,expiry_time expiryTime,payment_interest_type paymentInterestType,`status`,`remark`,uplink_address uplinkAddress FROM open_list ";
        return getForList(sql);
    }

    @Override
    public TicketEnterInsti findDetailByNode(String no) throws Exception {
        String sql="SELECT tid,`no`,enterpriseName,enterpriseCode,acquirerName,\n" +
                "acquirerCode,amount,instutityName,create_time createTime,\n" +
                "expiry_time expiryTime,payment_interest_type paymentInterestType,\n" +
                "`status`,uplink_address uplinkAddress, `remark`\n" +
                "FROM open_list\n" +
                "WHERE NO=?";
        return get(sql,no);
    }


    @Override
    public int updateStatusByNo(String no) throws Exception {
        String sql = "UPDATE  ticket_open SET `status`='C' WHERE no=?";
        return update(sql, no);
    }

    @Override
    public List<TicketEnterInsti> selectViewByCondition(String no, String acquirerName, String minamount, String maxamount, Date createTime, String enterpriseName) throws Exception {
        List queryList = new ArrayList();
        String sql = "select no, acquirerName,amount,enterpriseName,instutityName,create_time createTime,expiry_time expiryTime,uplink_address uplinkAddress from open_list where 1=1 ";
        if (!StringUtils.isNullOrEmpty(no)) {
            sql += "and no like \"%\"?\"%\"";
            queryList.add(no);
        }
        if (!StringUtils.isNullOrEmpty(acquirerName)) {
            sql += "and acquirerName=?";
            queryList.add(acquirerName);
        }
        if (!StringUtils.isNullOrEmpty(enterpriseName)) {
            sql += "and enterpriseName=?";
            queryList.add(enterpriseName);
        }
        if (!StringUtils.isNullOrEmpty(minamount) || !StringUtils.isNullOrEmpty(maxamount)) {
            sql += "and amount >=? and amount <=?";
            queryList.add(minamount);
            queryList.add(maxamount);

        }
        if (!Objects.isNull(createTime)) {
            sql += "and  create_time= ?";
            queryList.add(createTime);
        }
        System.out.println(sql);
        return getForList(sql, queryList.toArray());
    }

}
